package smartwiz.client.fwk.widgets;

import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

/**
 *	Affinis hyperlink
 */
public class Hyperlink extends HTML {
	
    private final static MouseOverHandler mouseOverHandler = new MouseOverHandler() {
        @Override
        public void onMouseOver(MouseOverEvent event) {
            ((Widget) event.getSource()).getElement().getStyle().setColor("black");
        }
    };
    private final static MouseOutHandler mouseOutHandler = new MouseOutHandler() {
        @Override
        public void onMouseOut(MouseOutEvent event) {
            ((Widget) event.getSource()).getElement().getStyle().setColor("blue");
        }
    };
    
    private final static MouseOutHandler visitedMouseOutHandler = new MouseOutHandler() {
        @Override
        public void onMouseOut(MouseOutEvent event) {
            ((Widget) event.getSource()).getElement().getStyle().setColor("red");
        }
    };

    private HandlerRegistration handler;
	
	public Hyperlink() {
        this("", null);
    }
	

    public Hyperlink(String title, ClickHandler clickHandler) {
		this(title, clickHandler, false);
	}
    
    public Hyperlink(String title, ClickHandler clickHandler, boolean visited) {
		super(title);
        addMouseOverHandler(mouseOverHandler);
        if(visited){
            getElement().getStyle().setColor("red");
        	handler = addMouseOutHandler(visitedMouseOutHandler);
        }else{	        
        	getElement().getStyle().setColor("blue");
        	handler = addMouseOutHandler(mouseOutHandler);
        }
        addStyleName("hand");
        getElement().getStyle().setTextDecoration(Style.TextDecoration.UNDERLINE);
        if(clickHandler != null){
        	addClickHandler(clickHandler);
        }
	}

    public void setVisited(boolean visited){
    	handler.removeHandler();
    	if(visited){
            getElement().getStyle().setColor("red");
        	handler = addMouseOutHandler(visitedMouseOutHandler);
    	}else{        
    		getElement().getStyle().setColor("blue");
        	handler = addMouseOutHandler(mouseOutHandler);
    	}
    }


}
